#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#define NN 64

// 用法：./client 127.0.0.1 8888
int main(int argc, char const* argv[])
{
    char buf[NN]={'\0'};
    int client_socket = socket(AF_INET,SOCK_STREAM,0);
    if (client_socket==-1)
    {
        perror("socket err\n");
        return -1;
    }

    struct sockaddr_in server_addr;
    server_addr.sin_family=AF_INET;
    server_addr.sin_port=htons(atoi(argv[2]));
    server_addr.sin_addr.s_addr=inet_addr(argv[1]);
    if(connect(client_socket,(struct sockaddr*)&server_addr,sizeof(server_addr))==-1)
    {
        perror("connect err\n");
        return -1;
    }

    while (1)
    {
        printf("请输入要输入的数据:\n");
        scanf("%s",buf);
        if (strcmp("quit",buf)==0)
        {
            break;
        }

        send(client_socket,buf,NN,0);

        recv(client_socket,buf,NN,0);
        printf("recv data:%s\n",buf);
    }
    
    close(client_socket);

    return 0;
}